﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SurveySez.Model;
using SurveySez.NavigationModel;
using System.Windows.Input;
using UpdateControls.XAML;

namespace SurveySez.ViewModel.Editor
{
    public class DashboardViewModel
	{
		private Game _game;
		private GameNavigation _gameNavigation;
		private IGamePresenter _gamePresenter;

		public DashboardViewModel(Game game, GameNavigation gameNavigation, IGamePresenter gamePresenter)
		{
			_game = game;
			_gameNavigation = gameNavigation;
			_gamePresenter = gamePresenter;
		}

		public string Question
		{
			get { return _gameNavigation.CurrentRound.Survey.Question; }
		}

		public IEnumerable<AnswerViewModel> Answers
		{
			get
			{
				return _gameNavigation.CurrentRound.Survey.Responses
					.OrderBy(r => r.Order)
					.Select((a, i) => new AnswerViewModel(i, a, _gameNavigation));
			}
		}

		public bool IsIncorrectPending
		{
			get { return _gameNavigation.CurrentRound.IsIncorrectPending(); }
		}

		public void GuessAnswer(int guess)
		{
			try
			{
				Response answer = _gameNavigation.CurrentRound.Survey.Responses
					.OrderBy(r => r.Order)
					.ElementAt(guess);
				_gameNavigation.CurrentRound.SetPendingCorrectOutcome(answer);
			}
			catch (Exception ex)
			{
				// Ignore.
			}
		}

		public void Commit()
		{
			_gameNavigation.CurrentRound.Commit();
		}

		public void Abort()
		{
			_gameNavigation.CurrentRound.Abort();
		}

		public void GuessIncorrect()
		{
			_gameNavigation.CurrentRound.SetPendingIncorrectOutcome();
		}

		public void NextRound()
		{
			_gameNavigation.NextRound();
		}

		public void PreviousRound()
		{
			_gameNavigation.PreviousRound();
		}

		public ICommand EndGame
		{
			get
			{
				return MakeCommand
					.Do(() => _gamePresenter.GameInPlay = null);
			}
		}
	}
}
